# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=qtwebkit
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_pkgver=5.212.0-alpha2
pkgver=5.212.0alpha2
pkgrel=4
pkgdesc="Webkit module for Qt5 (mingw-w64)"
arch=('any')
url="https://github.com/annulen/webkit/wiki"
options=(!strip)
license=(GPL3 LGPL3 FDL custom)
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "dos2unix"
             "gperf")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libxslt"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-qt5")
#install=${_realname}-qt5-git-${CARCH}.install
source=(https://github.com/annulen/webkit/releases/download/qtwebkit-${_pkgver}/${_realname}-${_pkgver}.tar.xz
        0001-gettickcount64-compatibility-xp.patch
        0003-qtwebkit-mfence-mingw.patch
        0004-msvc-bug-not-mingw.patch
        0005-fix-icu-find.patch
        0006-python-output-unix-line-endings.patch
        0008-fix-using-msys-perl.patch
        0009-missing-includes.patch
        0011-mingw-posix-layout-files.patch
        0014-disable-asm-win64.patch
        0201-add-proper-fullscreen-support.patch
        0202-install-private-headers-and-modules.patch
        0203-add-qtmultimedia-dependencies.patch
        0204-fix-build-with-icu-59.1.patch
        0205-make-size-debug-dll-tolerable.patch
        0300-fix-generating-module-files.patch)
sha256sums=('f8f901de567e11fc5659402b6b827eac75505ff9c5072d8e919aa306003f8f8a'
            '0c3e99ab770752b41782fed1b551353ebdee2b8f8c1c408b09b6d5550eb43dc1'
            'ed80d9a31cb1f9565841e3e206d76810881b7fce30210022270bd19694a9c906'
            'd6340dbcf58b3c031735c23d63e1c58d14baf6fbf2b085cbb7110ea81ade6b1f'
            'b9e39597d140f3fc40d07ae0f0eea0cbabd7b16d8e430d26445ae3063b8ad055'
            '16410ad976524b40e7b9d906e67e4f46b86b58db6d7bb61e929f500387d7937f'
            '9ef33dc05e5652362e10d34d75fd2cf9e7952c1c99306eb2402367401f86770b'
            'e365ccf6748b0ad1a4cba3dcc35fdb286cddc553b54e1242908a5b4019fd6d41'
            'a1ef305a81b4e1b70290d4c3f70bee545f06c17736fe77abde474dba0df8c5c7'
            'c39b65235a61217bf4197285d21765a47a91d2e8c267f4b777c982d56d32b408'
            '16c97176def8cfc035c4a9bc1be341b3932c05fdc1658090a7ec743d87ba88f0'
            '0ffb712530e45a4223cceed77fa9314d160b7df17f116fdf2a3ab6aea2d57b1a'
            'de80d99c50c8c23fbc83c4685e4916451535b59d72151c7752e73354aca87390'
            '21ed6c2ff873f873a1337d25537525901fadeb4ca3ef960775ad910aa937a545'
            '46843a860c5c24dd34f0ee7e9a27491b9a8656b2bc32e9bb348157febae899bc'
            '5ab5de5ee65fcd2a00412c8a2a1b2bbc4281d4ad99bac743757e6e8199a75ebe')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"
  patch -p1 -i ${srcdir}/0001-gettickcount64-compatibility-xp.patch
  patch -p1 -i ${srcdir}/0003-qtwebkit-mfence-mingw.patch
  patch -p1 -i ${srcdir}/0004-msvc-bug-not-mingw.patch
  patch -p1 -i ${srcdir}/0005-fix-icu-find.patch
  patch -p1 -i ${srcdir}/0006-python-output-unix-line-endings.patch
  patch -p1 -i ${srcdir}/0008-fix-using-msys-perl.patch
  patch -p1 -i ${srcdir}/0009-missing-includes.patch
  patch -p1 -i ${srcdir}/0011-mingw-posix-layout-files.patch
  patch -p1 -i ${srcdir}/0014-disable-asm-win64.patch
  
  # Upstream patches
  patch -p1 -i ${srcdir}/0201-add-proper-fullscreen-support.patch
  patch -p1 -i ${srcdir}/0202-install-private-headers-and-modules.patch
  patch -p1 -i ${srcdir}/0203-add-qtmultimedia-dependencies.patch
  patch -p1 -i ${srcdir}/0204-fix-build-with-icu-59.1.patch
  patch -p1 -i ${srcdir}/0205-make-size-debug-dll-tolerable.patch
  
  patch -p1 -i ${srcdir}/0300-fix-generating-module-files.patch

  # MSYS2 gperf cannot handle \r\n.
  find . -name "*.gperf" -exec dos2unix "{}" \;
}

build() {

  msg2 "Build debug version ..."
  [[ -d ${srcdir}/build-${MINGW_CHOST}-debug ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-debug
  mkdir ${srcdir}/build-${MINGW_CHOST}-debug
  cd ${srcdir}/build-${MINGW_CHOST}-debug

  local jit_opt="-DENABLE_JIT=ON"
  if [ "${CARCH}" = "x86_64" ]; then
    local jit_opt="-DENABLE_JIT=OFF"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Debug \
    -DPORT=Qt \
    ${jit_opt} \
    ../${_realname}-${_pkgver}

  make

  msg2 "Build release version ..."
  [[ -d ${srcdir}/build-${MINGW_CHOST}-release ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-release
  mkdir ${srcdir}/build-${MINGW_CHOST}-release
  cd ${srcdir}/build-${MINGW_CHOST}-release

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DPORT=Qt \
    ${jit_opt} \
    ../${_realname}-${_pkgver}

  make

}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}-debug
  make DESTDIR="${pkgdir}" install

  cd ${srcdir}/build-${MINGW_CHOST}-release
  make DESTDIR="${pkgdir}" install
}
